package exsun.jt808.data.media.operate.http;

import exsun.jt808.data.media.data.MediaStreamType;
import exsun.jt808.data.util.HttpUtil;
import exsun.jt808.data.media.data.CloseVideoRecord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Calendar;
import java.util.HashMap;

/**
 *
 * @author houzw
 *
 */
@SuppressWarnings("deprecation")
public class MediaCloseOrder {


	private HashMap<String, CloseVideoRecord> closeVideoRecords = new HashMap<>();
	private final static Logger logger = LoggerFactory.getLogger(MediaCloseOrder.class);
	private final static int connTimeout = 60000;
	private final static int readTimeout = 60000;

	public  String getCloseVideoUrl(String url, String phone, int channal) {
		String urlCur = url + "VideoClose";
		String requrl = urlCur + "?phoneStr="+phone+"&logicChannelNo="+channal;
		return requrl;
	}

	public  String getCloseHisVideoUrl(String url, String phone, int channal) {
		String urlHis = url + "HisVideoClose";
		String requrl = urlHis + "?phoneStr="+phone+"&logicChannelNo="+channal;
		return requrl;
	}

	public  String send(String requrl) {
		String result = HttpUtil.sendPost(requrl, "", connTimeout, readTimeout, "UTF-8");
		return result;
	}

	public void initVideo(String phoneNum, int channel){
		initVideo(phoneNum, channel, MediaStreamType.cur);
		initVideo(phoneNum, channel, MediaStreamType.his);
	}

	public void initVideo(String phoneNum, int channel, MediaStreamType type){
		String key = GetKey(phoneNum, channel, type);
		if(!closeVideoRecords.containsKey(key))return;
		closeVideoRecords.remove(key);
	}

	public boolean closeVideo(String phoneNum, int channel, String url, MediaStreamType type){
		if(type == MediaStreamType.cur){
			//关闭实时视频
			return closeCurVideo(phoneNum, channel, url);
		}else if(type == MediaStreamType.his) {
			//关闭历史视频
			return closeHisVideo(phoneNum, channel, url);
		}
		return false;
	}

	public boolean closeVideo(String phoneNum, int channel, String url){
		//没有观众则执行关闭，因为无法分辨是历史视频还是实时视频，故暂时都执行关闭
		//关闭实时视频
		boolean closeCurVideo = closeCurVideo(phoneNum, channel, url);
		//关闭历史视频
		boolean closeHisVideo = closeHisVideo(phoneNum, channel, url);
		return closeCurVideo||closeHisVideo;
	}

	public boolean closeCurVideo(String phoneNum, int channel, String url){
		//关闭实时视频
		if(CanSend(phoneNum, channel, MediaStreamType.cur)){
			closeVideo(phoneNum, channel, MediaStreamType.cur, getCloseVideoUrl(url, phoneNum, channel));
			return true;
		}
		return false;
	}

	public boolean closeHisVideo(String phoneNum, int channel, String url){
		//关闭历史视频
		if(CanSend(phoneNum, channel, MediaStreamType.his)){
			closeVideo(phoneNum, channel, MediaStreamType.his, getCloseVideoUrl(url, phoneNum, channel));
		return true;
	}
		return false;
	}

	private void closeVideo(String phoneNum, int channel, MediaStreamType type, String url){
		String result = send(url);
		logger.info("+++++++++  close device video, url:{}, result:{}", url, result);
		CloseVideoRecord record = new CloseVideoRecord(phoneNum, channel, type);
		String key = GetKey(phoneNum, channel, type);
		closeVideoRecords.put(key, record);
	}


	private String GetKey(String phoneNum, int channel,MediaStreamType type){
		return phoneNum+"_"+channel+"_"+type;
	}

	/**
	 * 若5s还是没有视频请求，则关闭
	 * 因为 会有设备数据已经上传上来，但是订阅者还没连上来的情况，所以判断关闭视频的时候，要延时一定时间
	 * @param phoneNum
	 * @param channel
	 * @param type
	 * @return
	 */
	private boolean CanSend(String phoneNum, int channel,MediaStreamType type)
	{
		int time = 5;//s
		String key = GetKey(phoneNum, channel, type);
		if(!closeVideoRecords.containsKey(key)){
			CloseVideoRecord record = new CloseVideoRecord(phoneNum, channel, type);
			closeVideoRecords.put(key, record);
			return false;
		}
		CloseVideoRecord record = closeVideoRecords.get(key);
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.SECOND, -time);
		if(record.getCloseTime().before(calendar)){
			closeVideoRecords.remove(key);
			return true;
		}
		return false;
	}
}
